import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class J101702 {
    public static List<List<Integer>> fourSum(int[] nums, int target) {
        Arrays.sort(nums);
        List<List<Integer>> list = new ArrayList<>();
        for(int i = 0; i < nums.length - 3; i++){
            long target1 = target - nums[i];
            for(int j = i + 1; j < nums.length - 2; j++){
                int m = j + 1;
                int n = nums.length - 1;
                long por = target1 - nums[j];
                while(m < n){
                    long sum = nums[m] + nums[n];
                    if(sum == por){
                        list.add(new ArrayList<>(List.of(nums[i], nums[j], nums[m], nums[n])));
                    }
                    if(sum <= por){
                        int tmp = nums[m];
                        while(m < nums.length && nums[m] == tmp){
                            m++;
                        }
                    }
                    if(sum >= por){
                        int tmp = nums[n];
                        while(n > m && nums[n] == tmp){
                            n--;
                        }
                    }
                }
                int tmp = nums[j];
                while(j < nums.length && nums[j] == tmp){
                    j++;
                }
            }
            int tmp = nums[i];
            while(i < nums.length && nums[i] == tmp){
                i++;
            }
        }
        return list;
    }

    public static void main(String[] args) {
        int[] nums = new int[]{1000000000,1000000000,1000000000,1000000000};
        System.out.println(fourSum(nums, -294967296).toString());
    }
}
